package cn.school.student.service.impl;

import cn.school.common.utils.SysUserUtils;
import cn.school.student.dao.StudentTrajectoryMapper;
import cn.school.student.entity.StudentTrajectory;
import cn.school.student.entity.vo.StudentTrajectoryExcel;
import cn.school.student.param.StudentTrajectoryPageParam;
import cn.school.student.service.StudentTrajectoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class StudentTrajectoryServiceImpl extends ServiceImpl<StudentTrajectoryMapper, StudentTrajectory> implements StudentTrajectoryService {

    @Resource
    StudentTrajectoryMapper studentTrajectoryMapper;

    @Override
    public List<StudentTrajectory> getStuTrajectory(Long studentId) {
//        String tenantId = SysUserUtils.getSysUser().getTenantId();
        Page page = new Page<>(1, 50);
        LambdaQueryWrapper<StudentTrajectory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentTrajectory::getStudentId,studentId).orderByDesc(StudentTrajectory::getFaceTime);
        IPage result = studentTrajectoryMapper.selectPage(page, wrapper);
        return result.getRecords();
    }

    @Override
    public IPage<StudentTrajectory> findPageList(StudentTrajectoryPageParam query) {
        String tenantId = SysUserUtils.getSysUser().getTenantId();
        query.setTenantId(tenantId);
        int startNum = (query.getPageNum() - 1 ) * query.getPageSize();

        int count = studentTrajectoryMapper.findPageCount(query);
        List<StudentTrajectory> list = studentTrajectoryMapper.findPageList(startNum,query);
        IPage<StudentTrajectory> page = new Page<>();
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public IPage<StudentTrajectory> teacherFindPageList(StudentTrajectoryPageParam query) {
        String tenantId = SysUserUtils.getSysUser().getTenantId();
        query.setTenantId(tenantId);
        int startNum = (query.getPageNum() - 1 ) * query.getPageSize();
        int count = studentTrajectoryMapper.teacherFindPageListCount(query);
        List<StudentTrajectory> list = studentTrajectoryMapper.teacherFindPageList(startNum,query);
        IPage<StudentTrajectory> page = new Page<>();
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<StudentTrajectoryExcel> exportTrajectory(StudentTrajectoryPageParam query) {
        query.setTenantId(SysUserUtils.getSysUser().getTenantId());
        return studentTrajectoryMapper.exportTrajectory(query);
    }
}
